package service;

import dao.database;
import pojo.Consumption;
import pojo.ConsumptionWithName;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class consumptionService {
    public static int insert(ConsumptionWithName consumption) {
        database d = new database();
        String sql = "INSERT INTO swim.consumption (card_id, money, info)\n" +
                "VALUES (" + consumption.getCardId() +
                ", " + consumption.getMoney() +
                ", '" + consumption.getInfo() +
                "');";
        System.out.println(sql);
        return d.update_execute(sql);
    }

    public static List<ConsumptionWithName> all() {
        database d = new database();
        String sql = "select * from consumption_with_name";
        ResultSet resultSet = d.executeQuery(sql);
        List<Object> list = database.selectList(new ConsumptionWithName(), resultSet);
        List<ConsumptionWithName> list1 = new ArrayList<>();
        list.forEach(i -> {
            list1.add((ConsumptionWithName) i);
        });
        return list1;
    }

//    public static List<HashMap<String, String>> statix() {
//        database d = new database();
//        String sql = "select card_id,sum(money) from consumption group by card_id;";
//        ResultSet resultSet = d.executeQuery(sql);
//        HashMap<String, String> tmp;
//        while (true){
//            try {
//                if (!resultSet.next()) break;
//                tmp = new HashMap<>();
//                tmp.
//                resultSet.getInt(0);
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//
//        }
//        List<Object> list = database.;
//        return list;
//    }

    public static List<ConsumptionWithName> queryByid(int id) {
        database d = new database();
        String sql = "select * from consumption_with_name where card_id=" + id;
        ResultSet resultSet = d.executeQuery(sql);
        List<Object> list = database.selectList(new ConsumptionWithName(), resultSet);
        List<ConsumptionWithName> list1 = new ArrayList<>();
        list.forEach(i -> {
            list1.add((ConsumptionWithName) i);
        });
        return list1;
    }

    public static List<Consumption> queryByid_delete(int id) {
        database d = new database();
        String sql = "select * from consumption where card_id=" + id;
        ResultSet resultSet = d.executeQuery(sql);
        List<Object> list = database.selectList(new Consumption(), resultSet);
        List<Consumption> list1 = new ArrayList<>();
        list.forEach(i -> {
            list1.add((Consumption) i);
        });
        return list1;
    }

    public static List<Consumption> queryByName(String name) {
        database d = new database();
        String sql = "select * from Consumption where name like '%" + name + "%'";
        System.out.println(sql);
        ResultSet resultSet = d.executeQuery(sql);
        List<Object> list = database.selectList(new Consumption(), resultSet);
        List<Consumption> list1 = new ArrayList<>();
        list.forEach(i -> {
            list1.add((Consumption) i);
        });
        return list1;
    }

    public static int deleteDeviceByid(int id) {
        database d = new database();
        String sql = "delete from Consumption where id=" + id;
        return d.update_execute(sql);
    }
}
